<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/pub/head_latest.inc"%>
<title>编辑</title>
<style type="text/css">
.l-form {
	padding-bottom: 20px;
}

.l-form li {
	line-height: 32px;
}

.l-form .l-fieldcontainer {
	padding: 5px 0;
}

.searchtitle {
    height: 30px;
}

.searchtitle span {
    font-size: 12px;
    font-weight: normal;
    vertical-align: middle;
}

.searchtitle img{
	width: 16px; 
	height: 16px;
	display: inline-block;
	vertical-align: middle;
}

.l-panel-bwarp {
    border-top: 0px;
}
</style>
</head>
<body style="padding-bottom: 31px;">
	<div id="tabcontainer" style="margin: 5px; margin-left: 3px;">
		<div title="基本配置" tabid="mainTab">
			<form id="mainform" method="post"></form>
		</div>
		<c:if test="${id != null}">
			<div title="SQL查询参数配置" tabid="paramsTab">
				<%@include file="../commsearchparams/commsearchparamsedit_edit.jsp"%>
			</div>

			<div title="钻取设置" tabid="linkParamTab" style="height: 500px;">
				<iframe src="${baseURL}/report/link/linklist/list.do?csId=${id}"></iframe>
			</div>

			<div title="表头设置" tabid="titlesParamsTab" style="height: 500px;">
				<iframe id="titlesParamsTabFrame" name="titlesParamsTabFrame"
					src="${baseURL}/report/metadataexpression/metadataexpressionlist/frame.do?csId=${id}"></iframe>
			</div>
		</c:if>
	</div>
	<script type="text/javascript">
  		var pkId = '${id}';
  		var reportType = null;
   		//tab
  		var tab = $("#tabcontainer").ligerTab({onBeforeSelectTabItem:function(tabid){
  			if(tabid != "mainTab"){
  				if(reportType != $("input[name='reportType']").val()){
  					$.ligerDialog.warn("修改了报表类型，请先保存！");
  					return false;
  				}
  			}
  		}});
         
        function badousoft(){}
		badousoft.ajax=function(s){
		    jQuery.ajax(s);
		}	
	
  		var baseUrl = '${baseURL}/report/commsearch/commsearch';
  		
      //button 
      //edit add
 	  var mainform = $("#mainform");
      mainform.ligerForm({ 
          fields: [
		     	{ name: "id", type: "hidden" },
		     	{ display: "报表名称", name: "name", labelAlign:"right",newline: true, labelWidth: 80, width: 220, space: 30, type: "text" ,
		    	 	validate: { required: true, maxlength: 50}},
		      	{ display: "报表编码", name: "code",labelAlign:"right", newline: false, labelWidth: 100, width: 220, space: 30, type: "text" ,
		    	 	validate: { required: true, maxlength: 50}},
		    	{ display: "标题", name: "title", labelAlign:"right",newline: false, labelWidth: 80, width: 220, space: 30, type: "text" ,
		     	 	validate: { required: true, maxlength: 50}},
		     	{name: "reportType", newline:true, labelWidth: 100, width: 220, space: 30, type: "hidden"},
	     	 	{ display: "模板类型", name: "reportTypeDesc", labelAlign:"right",newline: true, labelWidth: 80, width: 220, space: 30, type: "select" ,
		     		validate: { required: true},
		     		options: {valueFieldID: "reportType" ,data:[{id:1,text:'原生模板'},{id:2,text:'扩展模板'}], initValue:1, initText:"SQL报表"}},
		     	{name: "templateViewId", newline: false, labelWidth: 100, width: 220, space: 30, type: "hidden"},
		    	{ display: "展示模板", name: "templateViewValue", labelAlign:"right",newline: false, labelWidth: 100, width: 220, space: 30, type: "select" ,
		       		options: { valueFieldID: "templateViewId" , data:  ${templateList}}},
		  		{ display: "展示类别", name: "type", newline: false,labelAlign:"right", labelWidth: 80, width: 220, space: 30, type: "hidden" ,
		    	 	validate: { required: true, maxlength: 50}},
		    	{ display: "展示类别", name: "typeDesc", newline: false,labelAlign:"right", labelWidth: 80, width: 220, space: 30, type: "select" ,
		    	 		options: { valueFieldID: "type" , data:  ${showTypes}}},
		    	{ display: "功能按钮", name: "functionButton", newline: false, labelAlign:"right",labelWidth: 80, width: 220, space: 30, type: "hidden"},
		    	{ display: "功能按钮", name: "functionButtonDesc", newline: true, labelAlign:"right",labelWidth: 80, width: 220, space: 30, type: "select",
		    		options: { valueFieldID: "functionButton" ,isMultiSelect:true,split:",", data:  ${functionButtons}}},
		     	{ display: "排序", name: "priority", newline: false, labelAlign:"right", labelWidth: 100, width: 220, space: 30,type: "int" ,
		       		validate: { required: true, maxlength: 50}},
	       		{ display: "状态", name: "status", newline: false, labelAlign:"right",labelWidth: 80, width: 220, space: 30, type: "hidden" ,
		    	 	validate: { required: true, maxlength: 50}},
		    	{ display: "状态", name: "statusDesc", newline: false, labelAlign:"right",labelWidth: 80, width: 220, space: 30, type: "select" ,
		    	 		options: { valueFieldID: "status" , data:  ${reportStatus}}},
	       		{ display: "列表路径", name: "listJsonUrl", labelAlign:"right",newline: true, labelWidth: 80, width: 220, space: 30, type: "text", liCss:"userDefined"},
	       		{ display: "导出路径", name: "exportUrl", labelAlign:"right",newline: false, labelWidth: 100, width: 220, space: 30, type: "text", liCss:"userDefined"},
	       		{ display: "使用存储过程",name: "useProcedure", newline: false, labelAlign:"right", labelWidth: 85, width: 220, space: 30, type: "hidden"},
	       		{ display: "使用存储过程", name: "useProcedureDesc", newline: false,labelAlign:"right", labelWidth: 85, width: 220, space: 30, type: "select" ,
	    	 		options: { valueFieldID: "useProcedure" , data:  ${useProcedure}}},
	       		{ display: "sql", name: "sql", newline: true, labelAlign:"right",labelWidth: 80, width: 570, space: 30, type: "textarea", liCss:"sql"},
	       		{ display: "存储过程名称", name: "procedure", newline: true, labelAlign:"right",labelWidth: 85, width: 570, space: 30, type: "textarea" },
		     	{ display: "描述", name: "description", newline: true, labelAlign:"right",labelWidth: 80, width: 570, space: 30, type: "textarea" }],
		        toJSON: JSON2.stringify
      });
     
   //validate
   jQuery.metadata.setType("attr", "validate"); 
   LG.validate(mainform);
     
   $('<input type="button" value="保存" onclick="javascript:FF_Save()"  style="margin-left:100px;margin-top:10px;" class="l-button l-button-submit" />').appendTo(mainform);
   $('<input type="reset" value="取消" onclick="javascript:FF_Cancel()" style="margin-left:30px;margin-top:10px;" class="l-button l-button-test" />').appendTo(mainform);
	
     //save
     function FF_Save(){
      	  mainform.attr("action",baseUrl+"save/save.do?id=${id}");
      	  LG.submitForm(mainform, function (data) {
      		  var win = parent || window;
             if (data && data.hasOk) {
          	     parent.f_addTab("CommSearchEdit", '报表明细',  '${baseURL}/report/commsearch/commsearchedit/edit.do?menuId=${param.menuId}&id=' + data.bean.id);
             } else { 
			 	alert("操作失败，原因如下:\n"+data.message+"\n请联系系统管理员!");
             }
         });
    }
     //Cancel
    function FF_Cancel(){
    	var win = parent || window;
        win.LG.closeCurrentTab("CommSearchEdit");
    }
     //After Loaded
    function FF_AfterLoaded(data){
   	 	//do noting
   	 	reportType = data.reportType;
   	 	$("#sql").attr("placeholder","SELECT INFO.NAME AS username_用户名称,INFO.logon_id AS logonname_登录账号,U.org_name as orgname_部门 from sys_user U LEFT JOIN sys_user_comminfo INFO ON U.user_info_id = INFO.ID WHERE 1=1");
	   	
   	 	if(reportType == 1){
   	 	$("#doneGrid").ligerGrid({ 
	         columns: [ 
						 {display:"变量名称",name:"fieldName",width:120,type:"text",align:"center",isSort : false},
						 {display:"控件名称",name:"name",width:120,type:"text",align:"center",isSort : false},
	                    {display:"数据类型",name:"dataTypeStr",width:120,type:"text",align:"center",isSort : false},
	  	 		         {display:"操作类型",name:"operatorTypeStr",width:120,type:"text",align:"center",isSort : false},
	  	 		         {display:"数据字典值",name:"dicCode",width:120,type:"text",align:"center",isSort : false},
	  	 		         {display:"控件类型",name:"typeStr",width:120,type:"int",align:"center",isSort : false},
	  	 		         {display:"排序",name:"priority",width:120,type:"text",align:"center",isSort : false},
	  	 		         {display:"操作",name:"operate",width:120,type:"text",align:"center",isSort : false,
	  	 		        	  render:function(row){
	  	 		        		  var html = '<a style="color:blue;" onclick="editParams(\''+row.id+'\')">修改</a>  <a style="color:blue;"  onclick="delParams(\''+row.id+'\')">删除</a>';
	  	 		        		  return html;
	  	 		        	  }
	  	 		          }
	  	 		         ],
				dataAction: 'server',usePager:false,
				url:  '${baseURL}/report/commsearchparams/commsearchparamslist/listJSON.do?commsearchid=${id}', sortName: 'code',
				width: '98%', height: 200,heightDiff:-10, checkbox: false,
				pageParmName:"pageNo",pagesizeParmName:"perPageSize",
				rowHeight:23,rownumbers:true,isScroll:true,sortnameParmName:"sortName",
				sortorderParmName:"sortOrder"
			});
   	 	}else{
	   	 	$("#doneGrid").ligerGrid({ 
		         columns: [ 
							 {display:"变量名称",name:"fieldName",width:120,type:"text",align:"center",isSort : false},
							 {display:"控件名称",name:"name",width:120,type:"text",align:"center",isSort : false},
		                     {display:"定义搜索框",name:"searchBox",width:350,type:"text",align:"center",isSort : false},
		  	 		         {display:"搜索框数据来源",name:"searchDataSource",width:120,type:"text",align:"center",isSort : false,
		                    	 render:function(row,rowindex,value){
		                    		 if(value==1){
		                    			 return "数据字典";
		                    		 }else{
		                    			 return "自定义";
		                    		 }
		                    	 }},
		  	 		         {display:"默认值",name:"defaultValue",width:200,type:"int",align:"center",isSort : false},
		  	 		         {display:"排序",name:"priority",width:50,type:"text",align:"center",isSort : false},
		  	 		         {display:"操作",name:"operate",width:120,type:"text",align:"center",isSort : false,
		  	 		        	  render:function(row){
		  	 		        		  var html = '<a style="color:blue;" onclick="editParams(\''+row.id+'\')">修改</a>  <a style="color:blue;"  onclick="delParams(\''+row.id+'\')">删除</a>';
		  	 		        		  return html;
		  	 		        	  }
		  	 		          }
		  	 		         ],
				dataAction: 'server',usePager:false,
				url:  '${baseURL}/report/commsearchparams/commsearchparamslist/listJSON.do?commsearchid=${id}', sortName: 'code',
				width: '98%', height: 200,heightDiff:-10, checkbox: false,
				pageParmName:"pageNo",pagesizeParmName:"perPageSize",
				rowHeight:23,rownumbers:true,isScroll:true,sortnameParmName:"sortName",
				sortorderParmName:"sortOrder"
			});
   	 	}
   	 	
    }
     
    function getReportType(){
    	return reportType;
    }
 	
 	function genResults(){
	    var objArr = [];
		var sql = $("#sql").val();
		if(sql != null && sql != ""){
			 var strArr = sql.match(/[sS][eE][lL][eE][cC][Tt]([\w\W]{0,})[Ff][Rr][Oo][Mm]/)[1].split(",");
			 for(var i = 0 ; i < strArr.length ; i++){
		    	  var fieldName = '';
		    	  var displayName = '';
		    	  if(/\b[Aa][sS]/.test(strArr[i])){
		    		   var temp = strArr[i].match(/\b[Aa][sS]([\S\s]*)/)[1];
			    		   if(temp.lastIndexOf("_") > -1){
		    			   fieldName = temp.substr(0,temp.lastIndexOf("_"));
		    			   fieldName = fieldName.replace(/\s/,"");
			        		   displayName = temp.substr(temp.lastIndexOf("_")+1).replace(/\s\S/,"");
			    		   }
		    	  }else {
		    		  fieldName = strArr[i];
		    		  displayName = strArr[i];
		    	  }
		    	     var obj = {
		    	    		 id : fieldName,
		    	    		 text : displayName
		    	     };
		    	     objArr[i] = obj;
		     }
		}else{
			alert("当前尚未配置SQL语句，无法获取数据源！");
		}
			return objArr;
	}	

       //load data
    LG.loadForm({mainForm: mainform, 
  	  url:'${baseURL}/report/commsearch/commsearchedit/editJSON.do?id=${id}',callback: FF_AfterLoaded});
       
 	 var doneGrid = null;
 	 
	function reloadGrid(data){
		 if (data && data.hasOk) {  
              doneGrid.loadData();
         } else { 
		 	  alert("操作失败，原因如下:\n"+data.message+"\n请联系系统管理员!");
         }
	
	}
	
	
	function addParams(){
		$("#paramsEditFrame").attr("src","${baseURL}/report/commsearchparams/commsearchparamsedit/edit.do?commsearchid=${id}");
	}
	
	function editParams(id){
		$("#paramsEditFrame").attr("src","${baseURL}/report/commsearchparams/commsearchparamsedit/edit.do?commsearchid=${id}&id="+id);
	}
	
	function delParams(id){
		jQuery.ligerDialog.confirm('确认删除?', function (confirm) {
              if (confirm){
              	 $.ajax({
   					type: "GET",
   					url: "${baseURL}/report/commsearchparams/commsearchparamsdelete/delete.do",
   					dataType : "json",
   					data: "ids="+id,
   					beforeSend: function (){
		                LG.loading = true;
		                LG.showLoading('操作中，请耐心等待...');
		            },
		            complete: function (){
		                LG.loading = false;	  
		                LG.hideLoading();
		            },
   					success: function(msg){
     					if(msg && msg.hasOk){
     						 doneGrid.loadData();
     						 alert('操作成功!');
     					}else{
     						 alert("操作失败，原因如下:\n"+msg.message+"\n请联系系统管理员!");
     					}
   					}
 				 });
              }
        });
	}
 </script>
</body>
</html>